/*
 * @(#) PartInterService.java 2013-8-6 下午1:22:14
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.inter.erp;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.BillOfMaterials;
import com.datasweep.compatibility.client.BomItem;
import com.datasweep.compatibility.client.Part;
import com.datasweep.compatibility.client.PartFilter;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.pnuts.Functions;
import com.osi.mes.inter.BaseInterService;
import com.rockwell.ftpc.framework.common.XMLUtils;
import com.rockwell.ftpc.framework.common.XmlData;
import com.rockwell.mes.rtmis.middle.constants.IMDConstants;

public class BomInterService extends BaseInterService
{

	private static BomInterService instatnce = null;

	private BomInterService(Context context)
	{
		super(context);
	}

	private BomInterService(Functions functions)
	{
		super(functions);
	}

	public static BomInterService getInstance(Context context)
	{
		if (instatnce == null)
		{
			synchronized (BomInterService.class)
			{
				if (instatnce == null)
				{
					instatnce = new BomInterService(context);
				}
			}
		}
		return instatnce;
	}

	public Hashtable<String, Object> receiveBom(Hashtable<String, Object> receivedData)
	{
		Hashtable<String, Object> returnValue = new Hashtable<String, Object>();
		try
		{
			String msgObjType = (String) receivedData.get(IMDConstants.MD_TABLE_COULMN_MSGObJ_TYPE);
			if (!"66".equals(msgObjType))
			{
				throw new Exception(
					"The MSGObjType value does not matched, it should be 66 instead of "
						+ msgObjType);
			}
			String msgTransType = (String) receivedData
				.get(IMDConstants.MD_TABLE_COULMN_MSG_TRANS_TYPE);

			String msgKeyFields = (String) receivedData
				.get(IMDConstants.MD_TABLE_COULMN_MSG_KEY_FIELDS);
			String msgFieldsValue = (String) receivedData
				.get(IMDConstants.MD_TABLE_COULMN_MSG_FIELDS_VALUE);
			String msgContent = (String) receivedData.get(IMDConstants.MD_TABLE_COULMN_MSG_CONTENT);
			XmlData xmlData = XMLUtils.strToXMLData(msgContent);
			Hashtable<String, ArrayList<Object>> value = xmlData.getData();
			String bomRevision = getTransferValue(
				value, "Version");
			BillOfMaterials bom = getFunctions().getBom(
				msgFieldsValue, bomRevision);
			if (INTER_STR_CREATE.equalsIgnoreCase(msgTransType))
			{
				if (bom != null)
				{
					throw new Exception(
						"The BOM object has already existed in RTMIS, the ItemCode is "
							+ msgFieldsValue);
				}
				bom = getFunctions().createBOM();

				bom.setBomName(msgFieldsValue);
				bom.setBomRevision(bomRevision);
				bom.setUDA(
					getFunctions().createTime(
						getTransferValue(
							value, "EffectiveDate")), UDA_BOM_PLANNED_EFFECTIVE_DATE);
				bom.setUDA(
					toFloat(getTransferValue(
						value, "Quantity")), UDA_BOM_QUANTITY);
				bom.setUDA(
					INTER_CREATE, UDA_INTER_FLAG_SAP);
				ArrayList<Object> lines = (ArrayList<Object>) value.get("Lines");
				for (int i = 0; i < lines.size(); i++)
				{
					Hashtable<String, ArrayList<Object>> line = (Hashtable<String, ArrayList<Object>>) lines
						.get(i);
					String partNumber = getTransferValue(
						line, "ItemCode");
					Part part = getFunctions().getPart(
						partNumber, "1");
					if (part == null)
					{
						throw new Exception("The BomItem object did not exist, ItemCode is "
							+ partNumber);
					}
					BomItem bomItem = bom.addBomItem(part);
					bomItem.setQuantity(getFunctions().stringToBigDecimal(
						getTransferValue(
							line, "Quantity")));
				}
				Response response = bom.save();
				if (response.isError())
				{
					throw new Exception(response.getFirstErrorMessage());
				}
			}
			else if (INTER_STR_UPDATE.equalsIgnoreCase(msgTransType))
			{
				if (bom == null)
				{
					throw new Exception("The BOM object did not exist in RTMIS, the ItemCode is "
						+ msgFieldsValue + ", the Version is " + bomRevision);
				}

				bom.setUDA(
					INTER_UPDATE, UDA_INTER_FLAG_SAP);
				bom.setBomName(msgFieldsValue);
				bom.setBomRevision(bomRevision);
				bom.setUDA(
					getFunctions().createTime(
						getTransferValue(
							value, "EffectiveDate")), UDA_BOM_PLANNED_EFFECTIVE_DATE);
				bom.setUDA(
					toFloat(getTransferValue(
						value, "Quantity")), UDA_BOM_QUANTITY);
				bom.setUDA(
					INTER_CREATE, UDA_INTER_FLAG_SAP);
				Vector bomItems = bom.getBomItems();
				for (int i = 0; i < bomItems.size(); i++)
				{
					BomItem oldBomItem = (BomItem) bomItems.get(i);
					bom.removeBomItem(oldBomItem);
				}

				ArrayList<Object> lines = (ArrayList<Object>) value.get("Lines");
				for (int i = 0; i < lines.size(); i++)
				{
					Hashtable<String, ArrayList<Object>> line = (Hashtable<String, ArrayList<Object>>) lines
						.get(i);
					String partNumber = getTransferValue(
						line, "ItemCode");
					Part part = getFunctions().getPart(
						partNumber, "1");
					if (part == null)
					{
						throw new Exception("The BomItem object did not exist, ItemCode is "
							+ partNumber);
					}
					BomItem bomItem = bom.addBomItem(part);
					bomItem.setQuantity(getFunctions().stringToBigDecimal(
						getTransferValue(
							line, "Quantity")));
				}
				Response response = bom.save();
				if (response.isError())
				{
					throw new Exception(response.getFirstErrorMessage());
				}

			}
			else if (INTER_STR_DELETE.equalsIgnoreCase(msgTransType))
			{
				if (bom == null)
				{
					throw new Exception("The BOM object did not exist in RTMIS, the ItemCode is "
						+ msgFieldsValue + ", the Version is " + bomRevision);
				}
				bom.setUDA(
					INTER_DELETE, UDA_INTER_FLAG_SAP);
				Response response = bom.save();
				if (response.isError())
				{
					throw new Exception(response.getFirstErrorMessage());
				}
			}
			else
			{
				throw new Exception("The operation type is error.");
			}

			returnValue.put(
				IMDConstants.RESPONSE_STATUS, IMDConstants.RESPONSE_STATUS_SUCCESSFUL);
		}
		catch (Exception e)
		{
			returnValue.put(
				IMDConstants.RESPONSE_STATUS, IMDConstants.RESPONSE_STATUS_FAILED);
			returnValue.put(
				IMDConstants.RESPONSE_LAST_ERR_MSG, e.getMessage());
		}
		return returnValue;
	}
}
